package com.sjme.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sjme.domain.commonService.OptionGroup;
import com.sjme.domain.commonService.SelectOption;
import com.sjme.domain.commonService.dto.OptionGroupDto;
import com.sjme.domain.commonService.dto.SelectOptionDto;
import com.sjme.service.CommonService;
import com.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

import static com.constant.CommonConstant.OptionConstant.*;
import static com.constant.ResultConstant.ERROR;


@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private CommonService commonService;

    @PostMapping("/getSelectOptions")
    public Result getSelectOptions(@RequestBody List<String> optionGroupKeys) {
        try {
            return commonService.getSelectOptions(optionGroupKeys);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/queryOptionGroup")
    public Result queryOptionGroup(@RequestBody OptionGroupDto optionGroup) {
        try {
            IPage<OptionGroup> result = commonService.queryOptionGroup(optionGroup);
            return Result.success(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/addOptionGroup")
    public Result addOptionGroup(@RequestBody OptionGroup optionGroup) {
        try {
            if (commonService.addOptionGroup(optionGroup)){
                return Result.success("新增成功！");
            }else {
                throw new RuntimeException("新增失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/deleteOptionGroup")
    public Result addOptionGroup(@RequestBody List<String> optionGroupKeys) {
        try {
            String deleteFlag = commonService.deleteOptionGroup(optionGroupKeys);
            if (StringUtils.equals(DELETE_OPTION_GROUP_SUCCESS,deleteFlag)){
                return Result.success("删除成功！");
            }else if (StringUtils.equals(EXISTS_SELECT_OPTION,deleteFlag)){
                return Result.error(ERROR,"删除的组别数据存在字典数据！");
            }else {
                throw new RuntimeException("删除失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/updateOptionGroup")
    public Result updateOptionGroup(@RequestBody OptionGroup optionGroup) {
        try {
            String updateFlag = commonService.updateOptionGroup(optionGroup);
            if (StringUtils.equals(EXISTS_OPTION_GROUP_KEY,updateFlag)){
                throw new RuntimeException("修改的组别标识已存在！");
            }
            return Result.success(null);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/querySelectOption")
    public Result querySelectOption(@RequestBody SelectOptionDto selectOption) {
        try {
            IPage<SelectOption> result = commonService.querySelectOption(selectOption);
            return Result.success(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/addSelectOption")
    public Result addSelectOption(@RequestBody SelectOption selectOption) {
        try {
            if (commonService.addSelectOption(selectOption)){
                return Result.success("新增成功！");
            }else {
                throw new RuntimeException("新增失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/deleteSelectOption")
    public Result deleteSelectOption(@RequestBody List<String> selectOptionIds) {
        try {
            boolean deleteFlag = commonService.deleteSelectOption(selectOptionIds);
            if (deleteFlag){
                return Result.success("删除成功！");
            }else {
                throw new RuntimeException("删除失败！");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }

    @PostMapping("/updateSelectOption")
    public Result updateOptionGroup(@RequestBody SelectOption selectOption) {
        try {
            selectOption.setUpdateTime(LocalDateTime.now());
            boolean updateFlag = commonService.updateSelectOption(selectOption);
            if (updateFlag){
                return Result.success("修改成功！");
            }
            throw new RuntimeException("修改失败！");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(ERROR, e.getMessage());
        }
    }
}
